export function axiosMe({methods, url, params, data}){
    //方法转换大写
    methods = methods.toUpperCase()
    return new Promise((resolve,reject) => {
        //四步骤
        //1.创建对象
        const xhr = new XMLHttpRequest()
        //2.初始化
        //处理params对象 转成 a=100&&b=200
        let str = ''
        for(let k in params){
            str += `${k}=${params[k]}&`
        }
        str = str.slice(0,-1)
        xhr.open(methods,url+'?'+str)
        //3.发送
        if(methods === 'POST' || methods === 'PUT' || methods === 'DELETE'){
            //Content-type mine类型设置
            xhr.setRequestHeader('Content-type','application/json')
            //设置请求体
            xhr.send(JSON.stringify(data))
        }else{
            xhr.send()
        }  
        //4.处理结果
        xhr.onreadystatechange = function(){
            if(xhr.readyState === 4){
                if(xhr.status >= 200 && xhr.status < 300){
                    //成功状态的结果
                    resolve({
                        status:xhr.status,
                        message:xhr.statusText,
                        data:xhr.response
                    })
                }else{
                    reject(new Error('请求失败，失败的状态码为' + xhr.status))
                }
            }
        } 
        
    })
}

axiosMe.get = function (url,options){
    //发送ajax请求
    return axiosMe(Object.assign(options,{methods:'GET',url:url}))
}

axiosMe.post = function (url,options){
    //发送ajax请求
    return axiosMe(Object.assign(options,{methods:'POST',url:url}))
}

axiosMe.put = function (url,options){
    //发送ajax请求
    return axiosMe(Object.assign(options,{methods:'PUT',url:url}))
}
axiosMe.delete = function (url,options){
    //发送ajax请求
    return axiosMe(Object.assign(options,{methods:'DELETE',url:url}))
}